package org.idtem.utils.freeling;

import java.util.ArrayList;

public class AnalisisSintacticoNodoBaseFreeling 
{
	
	public static boolean IsHoja(String line)
	{
		return  line != null && (line.trim().startsWith("+(") || line.trim().startsWith("("));
	}
	
	public static boolean IsEndPathNode(String line)
	{
		return line != null && line.trim().equalsIgnoreCase("]");
	}
	
	public static boolean IsStartPathNode(String line)
	{
		return line != null && line.trim().endsWith("[");
	}

	public static ArrayList<AnalisisSintacticoNodoBaseFreeling> ObtenerInformacionSintactica(String[] split, int index, int[] nextIndex) throws Exception 
	{
		ArrayList<AnalisisSintacticoNodoBaseFreeling> result =  new ArrayList<AnalisisSintacticoNodoBaseFreeling>();
		Boolean isOpenNode = false;
		while (index <= split.length -1)
		{
			if (AnalisisSintacticoNodoBaseFreeling.IsStartPathNode(split[index]))
			{
				isOpenNode = true;
				AnalisisSintaticoNodoFreeling nodoPadre = new AnalisisSintaticoNodoFreeling(split[index]);
				nodoPadre.setHijos(ObtenerInformacionSintactica(split, index +1, nextIndex));
				result.add(nodoPadre);
				index = nextIndex[0];		
			}
			else if (AnalisisSintacticoNodoBaseFreeling.IsEndPathNode(split[index]))
			{
				if (isOpenNode)
				{
					index++;
					isOpenNode = false;
				}
				else
				{
					nextIndex[0] = index++;
					return result;
				}
			}
			else if (AnalisisSintacticoNodoBaseFreeling.IsHoja(split[index]))
			{
				result.add(new AnalisisSintacticoNodoHojaFreeling(split[index]));
				index++;
			}
			else if (split[index] != null && split[index].trim().isEmpty())
				index++;
			else
				throw new Exception();
		}
		return result;
	}
	
}
